<?php
namespace App\Http\Controllers;

use App\Models\ActPrize;
use App\Models\ActPrizeSetting;
use App\Models\GameGoods;
use Illuminate\Http\Request;
use App\Models\Act;
use App\Http\Requests\ActPrizeRequest;

class GameGoodsController extends CommonController
{
    /**
     *获取物品列表
     */
    public function getIndex()
    {
        $lists = GameGoods::orderBy('created_at', 'desc')->paginate(10);

//        dd($lists);

        return view('gamegoods.index')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     */
    public function search()
    {
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {

        return view('gamegoods.create');
    }

    /**
     * 执行一个创建
     */
    public function postCreate(Request $request)
    {
        $data = $request->input();
        $data['stock'] = $data['total'];

        unset($data['formType'], $data['_token']);
        $result = GameGoods::create($data);

        //生成这个奖的二维码
//        $url = '/game/web?id='.$result->id;
//        $file_name = unique_id() . '.png';
//        $qrCode = img_create($url, $file_name,$this->gameDir);

//        ActPrize::where('id', $result->id)->update(['qrcode_detail'=>$qrCode]);

        return AjaxCallbackMessage('奖品类型创建成功', true, url('gamegoods/index'));
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = GameGoods::where('id', $data['id'])->first();

//        dd($list);
        return view('gamegoods.edit')->with('list', $list);
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(Request $request)
    {
        $data = $request->input();
        $id = $data['id'];
        if($data['stockChange']>0){
            $data['total'] = $data['total']+$data['stockChange'];
        }
        $data['stock'] = $data['stock']+$data['stockChange'];
        unset($data['formType'], $data['_token'], $data['stockChange']);

        GameGoods::where('id', $id)->update($data);

//        if(!$prize->qrcode_detail){
//            //生成这个奖的二维码
//            $url = '/game/web?id='.$id;
//            $file_name = unique_id() . '.png';
//            $qrCode = img_create($url, $file_name, $this->gameDir);
//
//            ActPrize::where('id', $id)->update(['qrcode_detail'=>$qrCode]);
//        }

        return AjaxCallbackMessage('物品编辑成功', true, url('gamegoods/index'));
    }

    /**
     * 执行一个删除
     */
    public function postDelete()
    {
    }
}